const { src, dest, parallel, series, watch } = require('gulp')

const del = require('del')
const browserSync = require('browser-sync')

const bs = browserSync.create()


const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins()

const data = {
    menus: [{
            name: 'Home',
            icon: 'aperture',
            link: 'index.html'
        },
        {
            name: 'Features',
            link: 'features.html'
        },
        {
            name: 'About',
            link: 'about.html'
        },
        {
            name: 'Contact',
            link: '#',
            children: [{
                    name: 'Twitter',
                    link: 'https://twitter.com/w_zce'
                },
                {
                    name: 'About',
                    link: 'https://weibo.com/zceme'
                },
                {
                    name: 'divider'
                },
                {
                    name: 'About',
                    link: 'https://github.com/zce'
                }
            ]
        }
    ],
    pkg: require('./package.json'),
    date: new Date()
}

// const plugins.sass = require('gulp-sass')
// const plugins.babel = require('gulp-babel')
// const plugins.swig = require('gulp-swig')
// const plugins.imagemin = require('gulp-imagemin')

const clean = () => {
    return del(['dist'])
}

const style = () => {
    return src('src/assets/styles/*.scss', { base: 'src' })
        // .pipe(plugins.sass({outputStyle: 'expanded'}))    //完全展开    //并且不便宜_开头的scss文件
        .pipe(dest('dist'))
}

const script = () => {
    return src('src/assets/scripts/*.js', { base: 'src' })
        .pipe(plugins.babel({ presets: ['@babel/preset-env'] }))
        .pipe(dest('dist'))
}

const page = () => {
    // return src('src/**/*.html',{base:'src'})
    return src('src/*.html', { base: 'src' })
        .pipe(plugins.swig())
        .pipe(dest('dist'))
}
const image = () => {
    return src('src/assets/images/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
}

const font = () => {
    return src('src/assets/fonts/**', { base: 'src' })
        .pipe(plugins.imagemin())
        .pipe(dest('dist'))
}

const extra = () => {
    return src("public/**", { base: 'public' })
        .pipe(dest('dist'))
}

const serve = () => {
    watch('src/assets / styles.scss ', style)
    watch('src/assets/scripts/*.js', script)
    watch('src/*.html', page)
    bs.init({
        notify: false,
        // port:'2080',
        files: 'dist/**', //dist 文件热更新
        server: {
            baseDir: 'dist',
            routes: { // 优先走这个配置
                '/node_modules': 'node_modules'
            }
        }
    })
}

const compile = parallel(style, script, page, image, font)
const build = series(clean, parallel(compile, extra))
module.exports = {
    // style,
    // script,
    // page,
    // image,
    //font
    compile,
    build,
    serve
}